package com.co.user.service.impl;

import com.co.user.pojo.User;
import com.co.user.mapper.UserMapper;
import com.co.user.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 * UsUser业务层接口实现类
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    private Map<String, String> map;

    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @Override
    public boolean login(User user) {

        User user2 = userMapper.selectByPrimaryKey(user); //从数据库中查询出用户
        if (user2 == null) {
            return false;
        } else {
            boolean checkpw = BCrypt.checkpw(user.getPassword(), user2.getPassword());
            user2.setLastloginTime(new Date());
            userMapper.updateByPrimaryKeySelective(user2);
            return checkpw;


        }
    }

    /**
     * UsUser条件+分页查询
     *
     * @param user 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<User> findPage(User user, int page, int size) {
        //分页
        PageHelper.startPage(page, size);
        //搜索条件构建
        Example example = createExample(user);
        //执行搜索
        return new PageInfo<User>(userMapper.selectByExample(example));
    }

    /**
     * UsUser分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<User> findPage(int page, int size) {
        //静态分页
        PageHelper.startPage(page, size);
        //分页查询
        return new PageInfo<User>(userMapper.selectAll());
    }

    /**
     * UsUser条件查询
     *
     * @param user
     * @return
     */
    @Override
    public List<User> findList(User user) {
        //构建查询条件
        Example example = createExample(user);
        //根据构建的条件查询数据
        return userMapper.selectByExample(example);
    }


    /**
     * UsUser构建查询对象
     *
     * @param user
     * @return
     */
    public Example createExample(User user) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (user != null) {
            // 用户名唯一主键
            if (!StringUtils.isEmpty(user.getUsername())) {
                criteria.andLike("username", "%" + user.getUsername() + "%");
            }
            // 密码
            if (!StringUtils.isEmpty(user.getPassword())) {
                criteria.andEqualTo("password", user.getPassword());
            }
            // 昵称
            if (!StringUtils.isEmpty(user.getNickname())) {
                criteria.andLike("nickname", "%" + user.getNickname() + "%");
            }
            // 性别
            if (!StringUtils.isEmpty(user.getGender())) {
                criteria.andEqualTo("gender", user.getGender());
            }
            // 电话
            if (!StringUtils.isEmpty(user.getTelephone())) {
                criteria.andEqualTo("telephone", user.getTelephone());
            }
            // 邮箱
            if (!StringUtils.isEmpty(user.getEmail())) {
                criteria.andEqualTo("email", user.getEmail());
            }
            // 地区
            if (!StringUtils.isEmpty(user.getRegion())) {
                criteria.andEqualTo("region", user.getRegion());
            }
            // 用户头像地址
            if (!StringUtils.isEmpty(user.getUserImage())) {
                criteria.andEqualTo("userImage", user.getUserImage());
            }
            // 生日
            if (!StringUtils.isEmpty(user.getBirthday())) {
                criteria.andEqualTo("birthday", user.getBirthday());
            }
            // 最后登录时间
            if (!StringUtils.isEmpty(user.getLastloginTime())) {
                criteria.andEqualTo("lastloginTime", user.getLastloginTime());
            }
            // 个性签名
            if (!StringUtils.isEmpty(user.getSignature())) {
                criteria.andEqualTo("signature", user.getSignature());
            }
            // 创建时间
            if (!StringUtils.isEmpty(user.getCreateTime())) {
                criteria.andEqualTo("createTime", user.getCreateTime());
            }
            // 更新时间
            if (!StringUtils.isEmpty(user.getUpdateTime())) {
                criteria.andEqualTo("updateTime", user.getUpdateTime());
            }
        }
        return example;
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void delete(String id) {
        userMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改用户信息
     * @param user
     */
    @Override
    public void update(User user) {

        //获取生日
        Date birthday = user.getBirthday();
        //获取性别 0为男 1为女
        Integer gender = user.getGender();
        //获取国家
        String region = user.getRegion().toString();
        //获取昵称
        String nickname = user.getNickname();

        //从数据库查询user
        //User one = userMapper.selectOne(user);
        user.setGender(gender);
        user.setRegion(region);
        user.setBirthday(birthday);
        user.setNickname(nickname);
        user.setUpdateTime(new Date());

        userMapper.updateByPrimaryKeySelective(user);

    }

    /**
     * 增加UsUser
     *
     * @param user
     */
    @Override
    public void add(User user) {

        //获取盐
        String salt = BCrypt.gensalt();
        //对用户密码进行加密
        String hashpw = BCrypt.hashpw(user.getPassword(), salt);
        user.setPassword(hashpw);
        user.setCreateTime(new Date());
        userMapper.insert(user);
    }

    /**
     * 根据ID查询UsUser
     *
     * @param username
     * @return
     */
    @Override
    public User findById(String username) {
        return userMapper.selectByPrimaryKey(username);
    }

    /**
     * 查询UsUser全部数据
     *
     * @return
     */
    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }

    /**
     * 更改用户密码
     * @param
     */
    @Override
    public void updateUserPassword(User user) {

        userMapper.updateByPrimaryKey(user);

            }

    /**
     * 注册
     *
     * @param user
     */
    @Override
    public void signup(User user) {
        userMapper.insertSelective(user);
    }

    /**
     * 根据用户名查询订阅者数量
     * @param username
     * @return
     */
    @Override
    public List<String> findSubsByUsername(String username) {

        List<String> subsByUsername = userMapper.findSubsByUsername(username);

        return subsByUsername;
    }
}






